package com.example.demo.controller;

import com.example.demo.common.JwtUtils;
import com.example.demo.common.ResultModel;
import com.example.demo.common.ResultModelTool;
import com.example.demo.entity.Room_Picture;
import com.example.demo.entity.Roominfo;
import com.example.demo.entity.User;
import com.example.demo.service.RoominfoService;
import com.example.demo.service.UserService;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.Positive;
import java.util.List;
@CrossOrigin
@Controller
@RequestMapping("/roominfos")
@Transactional//事务
public class RoominfoController {
    @Resource
    RoominfoService roominfoService;


    @Resource
    private UserService userService;
    @Autowired
    HttpServletRequest request;
    @Autowired
    private JwtUtils jwtUtils;

    /**
     * 房间信息新增
     *
     * @param
     * @return
     */
    @PostMapping("")
    @ResponseBody
    public ResultModel insert(@RequestBody Roominfo roominfo) {
        String token = request.getHeader("Authorization");
        Claims claims = JwtUtils.parseJwt(token);
        String userId = claims.getId();
        User user1 = userService.getUser(userId);
        if (user1 == null) {
            return new ResultModel(200, "token过期或者权限不足");
        }
        ResultModel resultmodel = new ResultModel();
        resultmodel.setCode(200);
        roominfoService.insertroominfo(roominfo);
        resultmodel.setData("添加成功");
        return ResultModelTool.handleResultModel(resultmodel);
    }

    /**
     * 房间信息列表
     *
     * @param
     * @return
     */
    @GetMapping("")
    @ResponseBody
    public ResultModel roominfolist() {

        List<Roominfo> list = roominfoService.roominfolist();
        ResultModel resultModel = new ResultModel();
        resultModel.setCode(200);
        resultModel.setData(list);
        return ResultModelTool.handleResultModel(resultModel);
    }

    /**
     * 房间信息单个查询
     *
     * @param
     * @return
     */
    @GetMapping("/{no}")
    @ResponseBody
    public ResultModel selectroominfo(@PathVariable(value = "no") @Positive(message = "{no}") int no) {

        ResultModel resultModel = new ResultModel();
        resultModel.setCode(200);
        resultModel.setData(roominfoService.selectroominfo(no));
        return ResultModelTool.handleResultModel(resultModel);
    }

    /**
     * 房间信息修改
     *
     * @param
     * @return
     */
    @PutMapping("")
    @ResponseBody
    public ResultModel updateroominfo(@RequestBody Roominfo roominfo) {
        String token = request.getHeader("Authorization");
        Claims claims = JwtUtils.parseJwt(token);
        String userId = claims.getId();
        User user1 = userService.getUser(userId);
        if (user1 == null) {
            return new ResultModel(200, "token过期或者权限不足");
        }
        ResultModel resultModel = new ResultModel();
        resultModel.setCode(200);
        roominfoService.updateroominfo(roominfo);
        resultModel.setData("修改成功");
        return ResultModelTool.handleResultModel(resultModel);
    }

    /**
     * 房间信息删除
     *
     * @param
     * @return
     */
    @DeleteMapping("/{no}")
    @ResponseBody
    public ResultModel deleteroominfo(@PathVariable(value = "no") @Positive(message = "{no}") int no) {
        //roominfoService.deleteroominfo(id);
        String token = request.getHeader("Authorization");
        Claims claims = JwtUtils.parseJwt(token);
        String userId = claims.getId();
        User user1 = userService.getUser(userId);
        if (user1 == null) {
            return new ResultModel(200, "token过期或者权限不足");
        }
        ResultModel resultModel = new ResultModel();
        resultModel.setCode(200);
        roominfoService.deleteroominfo(no);
        resultModel.setData("删除成功");
        return ResultModelTool.handleResultModel(resultModel);

    }


    @GetMapping(value = "/search/{floor_id}")
    @ResponseBody
    public ResultModel selectroomlist(@PathVariable(value = "floor_id") @Positive(message = "{floor_id}") int floor_id) {
        //roominfoService.deleteroominfo(id);
        ResultModel resultModel = new ResultModel();
        resultModel.setCode(200);
        resultModel.setData(roominfoService.selectroomlist(floor_id));
        return ResultModelTool.handleResultModel(resultModel);

    }

    @GetMapping(value = "/search1")
    @ResponseBody
    public ResultModel selectroomlist1(@RequestParam  int no,@RequestParam int floor_id) {
        List<Room_Picture> wareFromBase = roominfoService.selectRoom_Pictures(no,floor_id);
        ResultModel resultModel = new ResultModel();
        resultModel.setCode(200);
        resultModel.setData(wareFromBase);
        return ResultModelTool.handleResultModel(resultModel);
    }

    @GetMapping(value = "/search2")
    @ResponseBody
    public ResultModel selectroomlist2(@RequestParam  int no,@RequestParam int floor_id) {
        Roominfo roomFromBase = roominfoService.selectroompicturebyid(no,floor_id);
        ResultModel resultModel = new ResultModel();
        resultModel.setCode(200);
        resultModel.setData(roomFromBase);
        return ResultModelTool.handleResultModel(resultModel);
    }

}


